<?php

class LRenren {
	
	/**
	 * 人人网影响力计算公式
	 */
	static private function calculateWeight($friends, $views){
		return round($friends*0.8 + $views*0.2);
	}
	
	/**
	 * 同步网络信息
	 */
	static public function connect($user_id, $email, $password){
		require_once("/lib/snoopy.class.php");
		$snoopy = new Snoopy();
		$submit_vars = array(
			"email" => $email,
			"password" => $password,
			"origURL" => "/profile.do",
		);		
		LSnoopyMask::mask(&$snoopy);
		if($snoopy->submit("http://3g.renren.com/profile.do", $submit_vars)){
			// 信息错误，登录失败
			if(preg_match('/<div class="error"><b>/', $snoopy->results) == 1){
				return false;
			}
			// 登录成功
			if(preg_match('/<div class="sec nav">/', $snoopy->results) == 1){
				$tags = preg_split('/<span class="gray">\(|\)<\/span><br \/></', $snoopy->results);
				$views = $tags[1];
				$friends = $tags[3];
				
				// 记录同步得来的网络信息
				$sync = Table::Fetch('sync_renren', $user_id);
				if($sync){
					return Table::UpdateCache('sync_renren', $user_id, array(
								'friends' => $friends,
								'views' => $views,
								'weight' => self::calculateWeight($friends, $views),
								'last_sync' => time(),
							));
				} else{
					$table = new Table('sync_renren', array(
									'id' => $user_id,
									'email' => $email,
									'password' => $password,
									'friends' => $friends,
									'views' => $views,
									'weight' => self::calculateWeight($friends, $views),
									'last_sync' => time(),
									'create_time' => time(),
								));
					return $table->insert(array('id','email','password','friends','views','weight','last_sync','create_time'));
				}
			}
		} else{
			return false;
		}
	}
	
	/**
	 * 发布消息
	 */
	static public function publish($user_id, $msg){
		// 发布信息
		$renrenTmp = Table::Fetch('sync_renren', $user_id);
		require_once("/lib/snoopy.class.php");
		$snoopy = new Snoopy();		
		$submit_vars = array(
			"email" =>  $renrenTmp['email'],
			"password" => $renrenTmp['password'],
			"origURL" => "/profile.do",
		);
		LSnoopyMask::mask(&$snoopy);
		if($snoopy->submit("http://3g.renren.com/profile.do", $submit_vars)){
			// 信息错误，登录失败
			if(preg_match('/<div class="error"><b>/', $snoopy->results) == 1){
				return array("ret"=>_FAIL, "errMsg"=>_RENREN_LOGIN_ERROR);
			}
			// 登录成功
			if(preg_match('/<div class="sec nav">/', $snoopy->results) == 1){
				$tags = preg_split('/<span class="gray">\(|\)<\/span><br \/></', $snoopy->results);
				$views = $tags[1];
				$friends = $tags[3];
				$tags = preg_split('/<form action="|" method="post">/', $snoopy->results);
				$submit_vars = array(
					"sour" => "profile",
					"status" => $msg,
					"update" => "发布",
				);
				LSnoopyMask::mask(&$snoopy);
				$snoopy->submit($tags[1], $submit_vars);
				if(preg_match('/<div class="notice_y">/', $snoopy->results) == 1){
					return Table::UpdateCache('sync_renren', $user_id, array(
							'friends' => $friends,
							'views' => $views,
							'weight' => self::calculateWeight($friends, $views),
							'last_sync' => time(),
						));
				}
				return false;
			}
		} else{
			return false;
		}
	}
}